/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Description: 集合通信建链诉求计算派生类实现文件(NHR算法)
 */

#include "calc_nhr_transport_req.h"

namespace hccl {
CalcNHRTransportReq::CalcNHRTransportReq(std::vector<std::vector<u32>> &subCommPlaneVector,
    std::vector<bool> &isBridgeVector, u32 userRank)
    : CalcTransportReqBase(subCommPlaneVector, isBridgeVector, userRank)
{
}

CalcNHRTransportReq::~CalcNHRTransportReq()
{
}

HcclResult CalcNHRTransportReq::CalcTransportRequest(const std::string &tag, TransportMemType inputMemType,
    TransportMemType outputMemType, const CommParaInfo &commParaInfo,
    std::vector<SingleSubCommTransport> &commTransport, u32 subUserRankRoot)
{
    (void)subUserRankRoot;
    u32 ringSize = subCommPlaneVector_.size();
    commTransport.resize(ringSize);

    for (u32 ringIndex = 0; ringIndex < ringSize; ringIndex++) {
        if (commParaInfo.commPlane == COMM_LEVEL1 && !isBridgeVector_[ringIndex]) {
            continue; // 跳出本次循环
        }

        u32 rank = GetSubCollectiveRank(subCommPlaneVector_[ringIndex]);
        if (rank == INVALID_VALUE_RANKID) {
            continue;
        }

        u32 rankSize = subCommPlaneVector_[ringIndex].size();
        SingleSubCommTransport &subCommTransport = commTransport[ringIndex];
        subCommTransport.transportRequests.resize(rankSize);
        // 只有一张卡时不需要建链
        if (rankSize == HCCL_RANK_SIZE_EQ_ONE) {
            HCCL_INFO("comm base needn't to create links, rankSize_[%u].", rankSize);
            return HCCL_SUCCESS;
		}

        for (u32 delta = 1; delta < rankSize; delta <<= 1) {
            const u32 targetRankPos = static_cast<u32>(rank + delta) % rankSize;
            TransportRequest &tmpTransport = subCommTransport.transportRequests[targetRankPos];
            tmpTransport.isValid = true;
            tmpTransport.localUserRank  = userRank_;
            tmpTransport.remoteUserRank = subCommPlaneVector_[ringIndex][targetRankPos];
            tmpTransport.inputMemType = inputMemType;
            tmpTransport.outputMemType = outputMemType;
            HCCL_INFO("[CommFactory][CalcNHRCommInfo] param_.tag[%s] ringIndex[%u], localRank[%u], \
                remoteRank[%u], inputMemType[%d], outputMemType[%d]", tag.c_str(), ringIndex, userRank_,
                tmpTransport.remoteUserRank, inputMemType, outputMemType);

            const u32 targetRankNeg = static_cast<u32>(rank + rankSize - delta) % rankSize;
            TransportRequest &tmpTransportNeg = subCommTransport.transportRequests[targetRankNeg];
            tmpTransportNeg.isValid = true;
            tmpTransportNeg.localUserRank  = userRank_;
            tmpTransportNeg.remoteUserRank = subCommPlaneVector_[ringIndex][targetRankNeg];
            tmpTransportNeg.inputMemType = inputMemType;
            tmpTransportNeg.outputMemType = outputMemType;
            HCCL_INFO("[CommFactory][CalcNHRCommInfo] param_.tag[%s] ringIndex[%u], localRank[%u], \
                remoteRank[%u], inputMemType[%d], outputMemType[%d]", tag.c_str(), ringIndex, userRank_,
                tmpTransportNeg.remoteUserRank, inputMemType, outputMemType);
        }
        subCommTransport.enableUseOneDoorbell = true;
    }
    return HCCL_SUCCESS;
}

}  // namespace hccl